﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;

public partial class Admin_AdminProductsIpod : System.Web.UI.Page
{
    string connectionString;
    private SqlConnection con;
    private SqlDataAdapter da;
    private SqlCommand cmd;
    private DataSet ds;

    protected void Page_Load(object sender, EventArgs e)
    {
        connectionString = WebConfigurationManager.ConnectionStrings["AppleShopConnectionString"].ToString();
        con = new SqlConnection(connectionString);
        con.Open();

        if (!IsPostBack)
        {
            view();
        }
    }

    protected void view()
    {
        try
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            ds = new DataSet();
            cmd = new SqlCommand("spGetAllProductIpod", con);
            cmd.CommandType = CommandType.StoredProcedure;
            da = new SqlDataAdapter(cmd);
            da.Fill(ds);
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);
        }
        finally
        {
            con.Close();
        }
    }

    public void delete(string ProductsID)
    {
        if (con.State == ConnectionState.Open)
        {
            con.Close();
        }
        con.Open();
            cmd = new SqlCommand("spGetDelete", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@ProductsID", SqlDbType.VarChar).Value = ProductsID;
            cmd.ExecuteNonQuery();
            con.Close();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string ProductsID = GridView1.DataKeys[e.RowIndex].Values["ProductsID"].ToString();
        delete(ProductsID);
        lblresult.Text = "Delete Successfully !!!!";
        view();
    }

    protected void ShowProducts(string id)
    {
        try
        {
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            ds = new DataSet();
            cmd = new SqlCommand("spGetSelectProductIpod", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add("@id", SqlDbType.VarChar).Value = id;
            cmd.ExecuteNonQuery();
            da = new SqlDataAdapter(cmd);
            da.Fill(ds);


            string ID = ds.Tables[0].Rows[0][0].ToString();
            string Name = ds.Tables[0].Rows[0][1].ToString();
            string Type = ds.Tables[0].Rows[0][2].ToString();
            string Price = ds.Tables[0].Rows[0][3].ToString();
            string Img = ds.Tables[0].Rows[0][4].ToString();
            lblresult.Text = "" + ID + " " + Name + " " + Type + " " + Price + " " + Img;
            Response.Redirect("AdminProductsIpodUpdate.aspx?ID=" + ID + "&Name=" + Name + "&Type=" + Type + "&Price=" + Price + "&Image=" + Img);

        }
        catch (SqlException ex)
        {
            throw new Exception(ex.Message);

        }
        finally
        {
            con.Close();
        }
    }
    protected void GridView1_RowEditing1(object sender, GridViewEditEventArgs e)
    {
        string ProductsID = GridView1.DataKeys[e.NewEditIndex].Values["ProductsID"].ToString();
        //lblresult.Text = ProductsID;
        ShowProducts(ProductsID);
    }
}